﻿using Entity.Service;
using System;
using System.Configuration;
using System.Text;
using Tools;

namespace BLL.Business
{
    public class Common
    {
        public static void UpdateConfig(string xKey, string xValue)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings[xKey].Value = xValue;
            cfa.Save();
        }

        public static string ConvertEncoding(string str)
        {
            string s = "";
            try
            {
                Encoding utf8 = Encoding.UTF8;
                Encoding gb2312 = Encoding.Unicode;
                byte[] gb2312Bytes = gb2312.GetBytes(str);
                byte[] utf8Bytes = Encoding.Convert(gb2312, utf8, gb2312Bytes);

                char[] asciiChars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];

                s = new string(asciiChars);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ConvertEncoding方法-编码转换出现异常：" + ex.Message,ex);
            }
            
             return s;
        }

        private static int _threadCount;
        public static int ThreadCount
        {
            get { return _threadCount; }
            set { _threadCount = value;
            }
        }


        public static ResponseCode GetNoSuccessCode(ResultType type)
        {
            ResponseCode code = ResponseCode.其他错误; ;
            switch (type)
            {
                case Entity.Service.ResultType.NoUserInfo:
                    code = ResponseCode.该用户不存在;
                    break;
                case Entity.Service.ResultType.ErrorState:
                    code = ResponseCode.特殊用户不交费;
                    break;
                case Entity.Service.ResultType.OrderFail:
                    code = ResponseCode.交易不合法;
                    break;
                case Entity.Service.ResultType.FailPay:
                    code = ResponseCode.交易失败;
                    break;
                case Entity.Service.ResultType.FailReversal:
                    code = ResponseCode.交易失败;
                    break;
                case Entity.Service.ResultType.ErrorReversal:
                    code = ResponseCode.其他错误;
                    break;
                case Entity.Service.ResultType.NoTransaction:
                    code = ResponseCode.交易不存在;
                    break;
                case Entity.Service.ResultType.HavePayInstruct:
                    code = ResponseCode.已缴费;
                    break;
                case Entity.Service.ResultType.HaveWaitCardOrder:
                    code = ResponseCode.有预交费未写卡记录;
                    break;
                case Entity.Service.ResultType.Paying:
                    code = ResponseCode.记录已锁;
                    break;
                case Entity.Service.ResultType.Charging:
                    code = ResponseCode.记录已锁;
                    break;
                case Entity.Service.ResultType.ErrorOther:
                    code = ResponseCode.其他错误;
                    break;
                case Entity.Service.ResultType.NotFoundDevinfo:
                    code = ResponseCode.其他错误;
                    break;
                case Entity.Service.ResultType.ErrorMoney:
                    code = ResponseCode.金额不符;
                    break;
                case Entity.Service.ResultType.LackParam:
                    code = ResponseCode.其他错误;
                    break;
                case Entity.Service.ResultType.ErrorParam:
                    code = ResponseCode.其他错误;
                    break;
                case Entity.Service.ResultType.ErrorMessage:
                    code = ResponseCode.数据包格式错;
                    break;
                case Entity.Service.ResultType.ExpireToken:
                    code = ResponseCode.数据包格式错;
                    break;
                case Entity.Service.ResultType.ErrorSignature:
                    code = ResponseCode.数据包格式错;
                    break;
                case Entity.Service.ResultType.Error:
                    code = ResponseCode.其他错误;
                    break;
            }

            return code;
        }

    }
}
